package com.pengtu.web.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.pengtu.Constants;
import com.pengtu.entity.LabelValue;
import com.pengtu.entity.app.Code;
import com.pengtu.service.app.SystemManager;
import com.pengtu.utils.LabelHelper;

/**
 * <p>
 * StartupListener class used to initialize and database settings and populate
 * any application-wide drop-downs.
 * 
 * <p>
 * Keep in mind that this listener is executed outside of
 * OpenSessionInViewFilter, so if you're using Hibernate you'll have to
 * explicitly initialize all loaded data at the Dao or service level to avoid
 * LazyInitializationException. Hibernate.initialize() works well for doing
 * this.
 * 
 * @author
 */
public class StartupListener extends ContextLoaderListener implements
		ServletContextListener {

	private static final Log log = LogFactory.getLog(StartupListener.class);

	@SuppressWarnings("unchecked")
	public void contextInitialized(ServletContextEvent event) {
		if (log.isDebugEnabled()) {
			log.debug("initializing context...");
		}

		// call Spring's context ContextLoaderListener to initialize
		// all the context files specified in web.xml
		super.contextInitialized(event);

		ServletContext context = event.getServletContext();

		// Orion starts Servlets before Listeners, so check if the config
		// object already exists
		Map<String,Object> config = (HashMap<String,Object>) context.getAttribute(Constants.CONFIG);

		if (config == null) {
			config = new HashMap<String,Object>();
		}

		if (context.getInitParameter(Constants.CSS_THEME) != null) {
			config.put(Constants.CSS_THEME, context
					.getInitParameter(Constants.CSS_THEME));
		}
//		ApplicationContext ctx = WebApplicationContextUtils
//				.getRequiredWebApplicationContext(context);
//		boolean encryptPassword = false;
////		try {
////			ProviderManager provider = (ProviderManager) ctx
////					.getBean("authenticationManager");
////			for (Iterator<AuthenticationProvider> it = provider.getProviders().iterator(); it.hasNext();) {
////				AuthenticationProvider p =  it.next();
////				if (p instanceof RememberMeAuthenticationProvider) {
////					config.put("rememberMeEnabled", Boolean.TRUE);
////				}
////			}
////
////			if (ctx.containsBean("passwordEncoder")) {
////				encryptPassword = true;
////				config.put(Constants.ENCRYPT_PASSWORD, Boolean.TRUE);
////				String algorithm = "SHA";
////				if (ctx.getBean("passwordEncoder") instanceof Md5PasswordEncoder) {
////					algorithm = "MD5";
////				}
////				config.put(Constants.ENC_ALGORITHM, algorithm);
////			}
////		} catch (NoSuchBeanDefinitionException n) {
////			// ignore, should only happen when testing
////		}

		context.setAttribute(Constants.CONFIG, config);
//
//		// output the retrieved values for the Init and Context Parameters
//		if (log.isDebugEnabled()) {
//			log
//					.debug("Remember Me Enabled? "
//							+ config.get("rememberMeEnabled"));
//			log.debug("Encrypt Passwords? " + encryptPassword);
//			if (encryptPassword) {
//				log.debug("Encryption Algorithm: "
//						+ config.get(Constants.ENC_ALGORITHM));
//			}
//			log.debug("Populating drop-downs...");
//		}

		setupContext(context);
	
	}

	public static void setupContext(ServletContext context) {
		
		// 码表: 
		setupCodeListContext(context);

		
	}
	
	public static void setupCodeListContext(ServletContext context){
        ApplicationContext ctx = 
            WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        SystemManager systemManager = (SystemManager) ctx.getBean("systemManager");
 		 List<Code> codesList =systemManager.getAvailableCodeList();
 		 Map<Integer,List<LabelValue>>  categoryMap= new  HashMap<Integer,List<LabelValue>>();
		 List<LabelValue> lavelValues = new ArrayList<LabelValue>();
			int falgCategory = 0;
 		 for (Code appCode : codesList) {
 			Integer category = appCode.getCategory();
 			if(falgCategory !=  category) {
 				categoryMap.put(falgCategory, lavelValues);	
 				falgCategory = category;	
 				lavelValues = new ArrayList<LabelValue>();
 			} 			
 	 			LabelValue lavelValue = new LabelValue(appCode.getCodeName(),appCode.getCodeId());
 	 			lavelValues.add(lavelValue);				
 		 }
 		 LabelHelper.buildLabelHelper(categoryMap);
	}

	


}
